package com.example.spider.provider.why10w.deprecated.wgwhy10;

import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.util.ReUtil;
import com.example.spider.provider.why10w.AbstractWhy10wListResultHandler;
import com.example.spider.provider.why10w.dto.Why10wDetailTask;
import com.example.spider.provider.why10w.dto.Why10wListTask;
import com.example.spider.task.SpiderTaskQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 处理列表查询结果
 *
 * @author lym
 */
@Slf4j
//@Component
public class Wgwhy10ListResultHandler extends AbstractWhy10wListResultHandler {

    private static final String DOMAIN_URL = "";
    @Override
    public void startSpiders() {
        for (Wgwhy10CategoryEnum w : Wgwhy10CategoryEnum.values()) {
            Why10wListTask listTask = createPageTask(w.name(), null, 1);
            SpiderTaskQueue.putTask(listTask);
        }
    }

    protected String buildPageTaskUrl(String firstCategory, String secondCategory, int pageNo) {
        String baseUrl = "https://www.10wgwhy.com/" + firstCategory + "/";
        return pageNo == 1 ? baseUrl : baseUrl + pageNo;
    }

    /**
     * 分析列表结果，创建下载详情任务
     */
    @Override
    public void doHandle(Why10wListTask task, String html) {

        log.debug("prepare handing Wgwhy10List " + task.getUrl());

        String hasContentRegex = "<div class=\"mainl\">(.*?)<!--@ mainl-->";
        List<String> contentStr = ReUtil.findAllGroup1(hasContentRegex, html);
        if (contentStr.isEmpty()) {
            // 爬完了都，会返回404页面，是没有内容的
            log.error("not a valid result. May finished!");
            return;
        }
        String content = contentStr.get(0);
        String urlAndTitleRegex = "<h2><a href=\"(.*?)\" target=\"_blank\">(.*?)</a></h2>";
        Pattern urlAndTitlePattern = PatternPool.get(urlAndTitleRegex, 32);

        // 一页大概10项
        List<Why10wDetailTask> detailTaskList = new ArrayList<>(15);
        ReUtil.findAll(urlAndTitlePattern, content, m ->
                // id 未设置
                detailTaskList.add(createDetailTask(task, null, m.group(2), m.group(1))));


        if (CollectionUtils.isEmpty(detailTaskList)) {
            log.warn("items is empty");
            return;
        }
        // 创建爬取详情的任务
        detailTaskList.forEach(SpiderTaskQueue::putTask);

        // 创建爬取下一页的任务
        String nextPageMark = ">››</a>";
        if (html.contains(nextPageMark)) {
            Why10wListTask nextPageTask = createPageTask(task.getFirstCategory(), task.getSecondCategory(), task.getPage() + 1);
            SpiderTaskQueue.putTask(nextPageTask);
        }

        repository.saveHtml(task, html);
    }

}
